package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.subview

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

class ProofCameroonStepHeader : LinearLayout {

    private lateinit var dataList: MutableList<JSONObject>
    private lateinit var adapter: ProofCameroonStepHeaderAdapter

    private lateinit var list: RecyclerView

    constructor(context: Context) : super(context) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initializeView(context)
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initializeView(context: Context) {
        inflate(context, R.layout.view_cameroon_proof_step_header, this)
        orientation = VERTICAL
        dataList = mutableListOf()
        list = findViewById(R.id.proofHeaderList)

        adapter = ProofCameroonStepHeaderAdapter(dataList)
        list.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        list.adapter = adapter
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setDataList(list: List<JSONObject>?) {
        if (list == null) {
            return
        }
        dataList.apply {
            clear()
            for (index in 0 until list.size) {
                val item = list[index]
                dataList.add(item)
            }
        }
        adapter.notifyDataSetChanged()
    }
}

/**
 * adapter
 */
class ProofCameroonStepHeaderAdapter(
    private val dataList: List<JSONObject>
) :
    RecyclerView.Adapter<ProofCameroonStepHeaderAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val title: TextView = view.findViewById(R.id.title)
        private val lineSelected: View = view.findViewById(R.id.lineSelected)

        fun setItem(item: String, checked: Boolean) {
            title.text = item
            if (checked) {
                lineSelected.visibility = View.VISIBLE
                title.setTextColor(Color.parseColor("#071B4D"))
                title.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            } else {
                lineSelected.visibility = View.GONE
                title.setTextColor(Color.parseColor("#999999"))
                title.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_cameroon_proof_step_header_adapter, parent, false)

        // 重置宽度
        val resources: Resources = ProjectApplication.instance.resources
        val dm: DisplayMetrics = resources.displayMetrics
        val itemWith = (dm.widthPixels - 64F.dpToPx()) / 3

        view.layoutParams.width = itemWith.toInt()
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item: JSONObject = dataList[position]
        val title = item.optString("title")
        val checked = item.optBoolean("done", false)
        holder.setItem(title, checked)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    // 将 dp 转换为像素
    private fun Float.dpToPx(): Float {
        return this * ProjectApplication.instance.resources.displayMetrics.density
    }
}